<?php
    use User\Model\LiveModel;
?>
<link href="/Public/css/common/switchery.css" rel="stylesheet" />
<style>
    .table>tbody>tr>td{vertical-align: middle}
</style>
<div id="page-wrapper">
    <div class="row">
        <div class="col-lg-12">
            <h1 class="page-header">评论室管理</h1>
        </div>
    </div>
    <div class="row">
        <div class="col-lg-12">
            <div class="panel">
                <div class="panel-heading" style="height:60px">
                    <!--<if condition="$support_comment_check eq LiveModel::SUPPORT_ACTIVE">-->
                        <!--<a href="javascript:;" id="passAll" class="btn btn-success">通过</a>-->
                    <!--<else/>-->
                        <!--<a href="javascript:;" id="deleteAll" class="btn btn-danger">删除</a>-->
                        <!--&lt;!&ndash;<a href="javascript:;" id="activateAll" class="btn btn-info">还原</a>&ndash;&gt;-->
                    <!--</if>-->
                    <!--<form class="input-group" action="/User/Live/comment" style="width:300px;float:right">-->
                        <!--<input type="hidden" name="topic_id" value="{$topic_id}">-->
                        <!--<input type="text" class="form-control" name="content" placeholder="输入评论内容搜索" value="{$content}">-->
                        <!--<span class="input-group-btn">-->
							<!--<button class="btn btn-default" type="submit">-->
								<!--<i class="fa fa-search"></i>-->
							<!--</button>-->
						<!--</span>-->
                    <!--</form>-->
                    <div class="form-group">
                        <label class="col-lg-2 col-sm-2 control-label" style="height:30px;line-height:30px;">是否开启评论</label>
                        <div class="col-lg-6">
                            <input type="checkbox" name="support_comment" class="js-switch" <if condition="$support_comment eq LiveModel::SUPPORT_ACTIVE">checked</if>/>
                        </div>
                    </div>
                </div>
                <div class="panel-body">
                    <div class="col-lg-12">
                        <div class="table-responsive">
                            <table class="table table-bordered">
                                <thead>
                                <tr>
                                    <th><input type="checkbox" id="selectAll"></th>
                                    <th>编号</th>
                                    <th>评论</th>
                                    <th>用户</th>
                                    <th>创建时间</th>
                                    <th>状态</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody>
                                <volist name="list" id="item">
                                    <tr>
                                        <td><input type="checkbox" name="ids" value="{$item.id}" data-chatid="{$item.id}"></td>
                                        <td>{$item.id}</td>
                                        <td>{$item.content}</td>
                                        <td>{$live_member[$item['openid']]}</td>
                                        <td>{$item.createdate}</td>
                                        <td>
                                            <if condition="$item.status eq LiveModel::COMMENT_APPLY">
                                                <span class="label label-warning">待审核</span>
                                            <elseif condition="$item.status eq LiveModel::COMMENT_ACTIVE"/>
                                                <span class="label label-success">正常</span>
                                            <else/>
                                                <span class="label label-default">已删除</span>
                                            </if>
                                        </td>
                                        <td>
                                            <if condition="$support_comment_check eq LiveModel::SUPPORT_ACTIVE">
                                                <if condition="$item.status eq LiveModel::COMMENT_APPLY">
                                                    <a type="button" href="javascript:;" onclick="return statusComment({$item['id']}, 'pass');" class="btn btn-success">通过</a>
                                                    <a type="button" href="javascript:;" onclick="return statusComment({$item['id']}, 'delete');" class="btn btn-danger">删除</a>
                                                <elseif condition="$item.status eq LiveModel::COMMENT_ACTIVE"/>
                                                    <a type="button" href="javascript:;" onclick="return statusComment({$item['id']}, 'delete');" class="btn btn-danger">删除</a>
                                                <else/>
                                                    <a type="button" href="javascript:;" onclick="return statusComment({$item['id']}, 'active');" class="btn btn-info">还原</a>
                                                </if>
                                            <else/>
                                                <if condition="$item.status eq LiveModel::COMMENT_APPLY">
                                                    <a type="button" href="javascript:;" onclick="return statusComment({$item['id']}, 'delete');" class="btn btn-danger">删除</a>
                                                <elseif condition="$item.status eq LiveModel::COMMENT_ACTIVE"/>
                                                    <a type="button" href="javascript:;" onclick="return statusComment({$item['id']}, 'delete');" class="btn btn-danger">删除</a>
                                                <else/>
                                                    <a type="button" href="javascript:;" onclick="return statusComment({$item['id']}, 'active');" class="btn btn-info">还原</a>
                                                </if>
                                            </if>
                                        </td>
                                    </tr>
                                </volist>
                                </tbody>
                            </table>
                        </div>
                        <div class="pages">
                            {$page}
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="/Public/js/common/switchery.js" ></script>
<script src="/Public/js/common/ios-init.js" ></script>
<script>
    //审核通过，手机端显示
    var wsServer = 'ws://118.31.50.232:7000';
    var ws;
    var isConnect;
    var status = true;
    var t;
    init();

    function init() {
        ws = new WebSocket(wsServer);
        isConnect = false;
        ws.onopen = function (evt) {
            onOpen(evt);
        };
        ws.onclose = function (evt) {
            onClose(evt)
        };
        ws.onmessage = function (evt) {
            onMessage(evt)
        };
        ws.onerror = function (evt) {
            onError(evt)
        };
    }

    function onOpen(evt) {
        isConnect = true;
        var topicId = '{$topic_id}';
        var json = JSON.stringify({'topicId': topicId, 'msgType': 0, 'auth': 1});
        ws.send(json);
    }

    function onClose(evt) {
        if (status) {
            t = setTimeout(function () {
                try {
                    init();
                } catch (err) {
                    alert("弹幕服务器繁忙！");
                }
            }, 1000);
        }
    }

    function onMessage(evt) {
        var data = JSON.parse(evt.data);
        switch (data.msgType) {
            case 0:     //人数
            case 1:     //文字
            case 2:     //点赞
            case 3:     //图文
            case 4:     //语音
            case 5:
            case 6:
            case 7:
            case 8:
            case 9:
                console.log(data);
                break;
        }
    }

    function onError(evt) {
        //console.log('Error occured: ' + evt.data);
    }

    $("#selectAll").click(function(){
        if($(this).prop('checked') == true){
            $("input[name=ids]").prop('checked',true);
        }else{
            $("input[name=ids]").prop('checked',false);
        }
    })

    $("#deleteAll").click(function(){
        if(!confirm('确认删除吗？')) return false;
        var ids = "";
        var topicId = '{$topic_id}';
        var chatId = 0;
        $("input[name=ids]").each(function() {
            if($(this).prop("checked")){
                ids += $(this).val() + "|";
                chatId = $(this).data('chatid');
                var json = JSON.stringify({'topicId':topicId,'msgType':5,'chatId':chatId});
                ws.send(json);
            }
        });
        if(ids != ''){
            $.ajax({
                data: {ids: ids},
                dataType: 'json',
                url: '/User/Live/deleteComment',
                type: 'get',
                success: function (res) {
                    alert(res['info']);
                    window.location.reload();
                }
            });
        }else{
            alert('请勾选评论！');
        }
    });

    $("#activateAll").click(function(){
        if(!confirm('确认还原吗？')) return false;
        var ids = "";
        $("input[name=ids]").each(function() {
            if($(this).prop("checked")){
                ids += $(this).val() + "|";
            }
        });
        if(ids != ''){
            $.ajax({
                data: {ids: ids},
                dataType: 'json',
                url: '/User/Live/activateComment',
                type: 'get',
                success: function (res) {
                    alert(res['info']);
                    window.location.reload();
                }
            });
        }else{
            alert('请勾选评论！');
        }
    });

    $("#passAll").click(function(){
        if(!confirm('确认通过吗？')) return false;
        var topicId = '{$topic_id}';
        var ids = "";
        var chatId = 0;
        $("input[name=ids]").each(function() {
            if($(this).prop("checked")){
                ids += $(this).val() + "|";
                chatId = $(this).data('chatid');
                var json = JSON.stringify({'topicId':topicId,'msgType':6,'chatId':chatId});
                ws.send(json);
            }
        });
        if(ids != ''){
            $.ajax({
                data: {ids: ids},
                dataType: 'json',
                url: '/User/Live/passComment',
                type: 'get',
                success: function (res) {
                    alert(res['info']);
                    window.location.reload();
                }
            });
        }else{
            alert('请勾选评论！');
        }
    });

    function statusComment(id, status){
        var topicId = '{$topic_id}';
        if(status == "active"){
            if(!confirm('确认还原吗？')) return false;
        }else if(status == "pass"){
            if(!confirm('确认通过吗？')) return false;
            var json = JSON.stringify({'topicId':topicId,'msgType':6,'chatId':id});
            ws.send(json);
        }else{
            if(!confirm('确认删除吗？')) return false;
            var json = JSON.stringify({'topicId':topicId,'msgType':5,'chatId':id});
            ws.send(json);
        }
        $.ajax({
            data: {id: id, status: status},
            dataType: 'json',
            url: '/User/Live/statusComment',
            type: 'get',
            success: function (res) {
                alert(res['info']);
                window.location.reload();
            }
        });
        return false;
    }

    $("input[name=support_comment]").on("change", function () {
        var topic_id = '{$topic_id}';
        $.ajax({
            data: {topic_id: topic_id},
            dataType: 'json',
            url: '/User/Live/supportComment',
            type: 'get',
            success: function (res) {
                alert(res['info']);
                var json = JSON.stringify({'topicId':topic_id,'msgType':9});
                ws.send(json);
            }
        });
    });
</script>